*' --------------
* sameness effects: main ----
* Appendix Table A2. robustness analysis using cross-sectional sample
*' ==============

clear all
* set up your working directory here
global home_dir ""

cd ${home_dir}

* predicted margins for interaction
cap: program drop margin_est
program margin_est
	args X k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_est_`mname'_`X'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_est_`mname'_`X'_`year'_`geo_type'.txt") replace

end 

cap: program drop margin_interact
program margin_interact 
	args X Y k mname year vv geo_type
	sum `X', d 
	local gap = (`r(max)' - `r(min)') / `k' 

	margin `Y' if e(sample), at(`X' = (`r(min)' (`gap') `r(max)')) predict(pr) post
	
	matrix A = r(table)
	matrix B = r(at)

	mat2txt, matrix(A) saving("./output/margins/`vv'_margins_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace
	mat2txt, matrix(B) saving("./output/margins/`vv'_at_interact_`mname'_`X'_`Y'_`year'_`geo_type'.txt") replace

end 

cap: program drop run_robustness
program run_robustness

args geo_type vv

use "./data/processed_notshared/cleaned_regtable_unemployment_`geo_type'_v2.dta", clear 

local year "all"

svyset [pw=weight] 	

if ("`vv'" == "v1"){
	keep if sample_v1 == 1	
}
if ("`vv'" == "v2") {
	keep if sample_v2 == 1	
}

local demographics_individual Female agegroup Race6 BornUSA 
local demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local contextual_control pop_density
local post_control_context RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5 RAT_PhysProb RAT_veteran


egen n_miss = rowmiss(Suic St `geo_type' Year `demographics_individual' `demographics_county' `contextual_control')
recode n_miss (0=0) (1/max=1)

drop if n_miss > 0

gen b_same_prop_empstat = std_same_prop_empstat > 0 if ~missing(std_same_prop_empstat)

*--------------
* Figure 3. interaction effects : Table S6, Model 1 to 4
*==============
local eq_demographics_individual Female i.agegroup i.Race6 BornUSA 
local eq_demographics_county RAT_Female RAT_AgeGrp4_2 RAT_AgeGrp4_3 RAT_AgeGrp4_4 RAT_Race6_2 RAT_Race6_3 RAT_Race6_4 RAT_Race6_5 RAT_Race6_6 RAT_BornUSA 
local eq_contextual_control pop_density
local eq_post_control RAT_EducGroup_1 RAT_EducGroup_2 RAT_EducGroup_3 RAT_EducGroup_4 RAT_EducGroup_5 RAT_PhysProb RAT_veteran
local eq_interact_context b2.empstat#c.pop_density b2.empstat#c.RAT_Female b2.empstat#c.RAT_AgeGrp4_2 b2.empstat#c.RAT_AgeGrp4_3 b2.empstat#c.RAT_AgeGrp4_4 b2.empstat#c.RAT_Race6_2 b2.empstat#c.RAT_Race6_3 b2.empstat#c.RAT_Race6_4 b2.empstat#c.RAT_Race6_5 b2.empstat#c.RAT_Race6_6 b2.empstat#c.RAT_BornUSA b2.empstat#c.pop_density b2.empstat#c.RAT_EducGroup_1 b2.empstat#c.RAT_EducGroup_2 b2.empstat#c.RAT_EducGroup_3 b2.empstat#c.RAT_EducGroup_4 b2.empstat#c.RAT_EducGroup_5 b2.empstat#c.RAT_PhysProb b2.empstat#c.RAT_veteran

local model_eq `eq_demographics_individual' `eq_demographics_county' `eq_contextual_control' 

estimates clear 

logit Suic i.St `model_eq' b2.empstat c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est1
	
logit Suic i.St `model_eq' b2.empstat i.EducGroup i.PhysProb i.veteran c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est2

logit Suic i.St `model_eq' b2.empstat i.EducGroup i.PhysProb i.veteran `eq_post_control' c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est3

logit Suic i.St `model_eq' b2.empstat i.EducGroup i.PhysProb i.veteran `eq_post_control' `eq_interact_context' c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est4

reghdfe Suic `model_eq' b2.empstat i.EducGroup i.PhysProb i.veteran  `eq_post_control' `eq_interact_context' c.std_same_prop_empstat [pw=weight], absorb(St) cluster(`geo_type')
estimates store est5

jacknife, dots cluster(`geo_type') : logit Suic i.St `model_eq' b2.empstat i.EducGroup i.PhysProb i.veteran `eq_post_control' `eq_interact_context' c.std_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est6

* binary treatment
logit Suic i.St `model_eq' b2.empstat c.b_same_prop_empstat [pw=weight], cluster(`geo_type')
estimates store est7

esttab est* using "./output/logit_model_v2_empstat_robustness_`geo_type'.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) mtitle

* marginal effects 
forvalues i = 1 / 6 {
	estimates restore est`i'
	margins, dydx(b2.empstat std_same_prop_empstat) post
	estimates store m`i' 
		
}
local i = 7
estimates restore est`i'
margins, dydx(b2.empstat b_same_prop_empstat) post
estimates store m`i' 
	
esttab m* using "./output/AME_model_v2_empstat_robustness_`geo_type'.csv", csv replace label nogap se star (+ 0.1 * 0.05 ** 0.01) mtitle

end

run_robustness cz "v2" 
run_robustness county "v2" 


